#!/bin/zsh
#+++ Description Start +++
# 读取ChangeLog.md中的版本信息
# 写入到plist文件中.
#=== Description End ===
targetName=$1
changeLogPath=$2
infoPlist=$3
# 验证changelog文件.
if [[ ! -e "${changeLogPath}" ]]; then
	echo "This project is versioned by change log file,but no change log file found!"
	exit -1
fi
echo "Change log file is valid"


# 验证info.plist,

if [[ ! -e ${infoPlist} ]]; then
	infoPlist=${PROJECT_DIR}/${INFOPLIST_FILE}
	if [[ ! -e ${infoPlist} ]]; then
		echo "No info plist file found!"
		exit -1
	fi
fi
echo "plist file is valid"


# 读取版本信息.
export tag="[${targetName}]"
# 满足第2个元素=tag的第一行.
currentVersion=$(awk -v tag="${tag}" 'BEGIN {currentTagIndex=0;version="";} {if($2 == tag){currentTagIndex++; if(currentTagIndex==1){version=""$3;version2=substr(version,2,length(version)-2) ;exit}}} END{print version2}' ${changeLogPath})
if [[ ${currentVersion} == "" ]]; then
	echo "invalid version"
	exit -1;
else
	echo "current version is:${currentVersion}"
fi

# 设置到plist文件.
/usr/libexec/PlistBuddy -c "Set :CFBundleShortVersionString $currentVersion" "${infoPlist}"


